package com.tjl.warehouse.controller;

import com.tjl.warehouse.common.R;
import com.tjl.warehouse.exception.ThrowUtils;
import com.tjl.warehouse.model.MyPage;
import com.tjl.warehouse.model.Store;
import com.tjl.warehouse.model.dto.StoreReq;
import com.tjl.warehouse.service.StoreService;
import com.tjl.warehouse.service.impl.StoreServiceImpl;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * @author Tang
 */
@RestController
@Slf4j
@Tag(name = "仓库接口")
//@AuthToken  //bug--使用该注解会导致StoreService无法注入
@RequestMapping("/store")
public class StoreController {

    @Resource
    private StoreService storeService;

    @GetMapping("/store-page-list")
    private R<MyPage<Store>> storePageList(StoreReq req,MyPage<Store> page) {

        ThrowUtils.throwIfParams(page == null);
        ThrowUtils.throwIfParams(page.getPageNum() <= 0, "页码最小为1");
        ThrowUtils.throwIfParams(page.getPageSize() > 500, "最大每页500条");

        if (storeService == null){
            storeService = new StoreServiceImpl();
        }

        return R.success(storeService.getPage(req,page));
    }

    @PutMapping("/store-update")
    public R<String> storeUpdate(@RequestBody Store store) {
        ThrowUtils.throwIfParams(store == null);

        boolean b = storeService.updateById(store);
        if (b) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    @DeleteMapping("/store-delete/{id}")
    public R<String> storeDelete(@PathVariable("id") Integer id) {
        ThrowUtils.throwIfParams(id == null);
        ThrowUtils.throwIfParams(id <= 0, "id最小为1");

        boolean b = storeService.removeById(id);
        if (b) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }


}
